<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>

        <el-col :span="12">
          <el-form-item :label="$t('入学时间')" prop="admissionTime">
            <el-date-picker
              v-model="form.admissionTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('入学时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('毕业时间')" prop="graduationTime">
            <el-date-picker
              v-model="form.graduationTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('毕业时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('学校')" prop="school">
            <el-input v-model="form.school" :placeholder="$t('学校')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('专业')" prop="major">
            <el-input v-model="form.major" :placeholder="$t('专业')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('学历')" prop="education">
            <el-input v-model="form.education" :placeholder="$t('学历')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="hr:education" />
    </div>
  </div>
</template>

<script>
import { saveHrBaseEducation, getHrBaseEducation } from '@/api/oa/hr/hrBaseEducation'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        admissionTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        graduationTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        school: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        major: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        education: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHrBaseEducation(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        hrId: undefined,
        admissionTime: undefined,
        graduationTime: undefined,
        school: undefined,
        major: undefined,
        education: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid && this.checkDate()) {
          this.loading = true
         this.$set(this.form, 'hrId', this.$route.query.hrId)
          saveHrBaseEducation(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.checkDate()
        }
      })
    },
    checkDate() {
      if (this.form.admissionTime === null || this.form.admissionTime === undefined || this.form.admissionTime === '' ||
          this.form.graduationTime === null || this.form.graduationTime === undefined || this.form.graduationTime === '') {
        return true
      }
      const timeFlag = new Date(this.form.admissionTime).getTime() > new Date(this.form.graduationTime).getTime()
      if (timeFlag) {
        this.$message({
          message: '入学时间不能大于毕业时间',
          type: 'warning'
        })
      }
      return !timeFlag
    }

  }

}
</script>
